package com.grandtech.insurance.common.bean;


import java.io.Serializable;
import java.util.Map;

/**
* @Description:    分页查询工具
* @Author:         han bo
* @CreateDate:     2019/6/19 15:39
* @UpdateUser:
* @UpdateDate:     2019/6/19 15:39
* @UpdateRemark:
* @Version:        1.0
*/

public class PageModel implements Serializable {

    private boolean pageAvailable = true;



    /**

     * 每页条数
     */
    private Integer pageSize = 30;
    /**
     * 当前页数
     */
    private Integer pageCur = 1;
    /**
     * 排序字段
     */
    private String sort;
    /**
     * 升序还是降序
     */
    private EnumOrder pageOrder;
    /**
     * 查询条件 k字段,v值只支持字符串的相等查询
     */
    private Map<String,String> condition;
    /**
     * 查询条件 sql 例："qhdm='qqqq'"
     */
    private String where ;
    /**
     * 数据库字段
     */
    private Integer limit = pageSize;//记录条数

    private Integer offset = 0;//位置

    private String order = pageOrder == null ? EnumOrder.asc.getName() : pageOrder.getName();//正序倒叙


    public boolean isPageAvailable() {
        return pageAvailable;
    }

    public void setPageAvailable(boolean pageAvailable) {
        this.pageAvailable = pageAvailable;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
        this.limit = pageSize;
    }

    public Integer getPageCur() {
        return pageCur;
    }

    public void setPageCur(Integer pageCur) {
        this.pageCur = pageCur;
        offset = getOffset();
    }

    public PageModel prePage() {
        pageCur = pageCur - 1 >= 1 ? pageCur - 1 : 1;
        return this;
    }

    public PageModel nextPage() {
        pageCur++;
        return this;
    }

    public int startIndex() {
        return (pageCur - 1) * pageSize;
    }

    public int endIndex() {
        return pageCur * pageSize - 1;
    }

    public int getLimit() {
        return pageSize;
    }

    public int getOffset() {
        return (pageCur - 1) * pageSize;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public EnumOrder getPageOrder() {
        return pageOrder;
    }

    public void setPageOrder(EnumOrder pageOrder) {
        this.pageOrder = pageOrder;
        setOrder(pageOrder == null ? EnumOrder.asc.getName() : pageOrder.getName());
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public enum EnumOrder {
        desc("desc"),
        asc("asc");
        String name;

        EnumOrder(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }


    public void setLimit(Integer limit) {
        this.limit = limit;
    }


    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public Map<String, String> getCondition() {
        return condition;
    }

    public void setCondition(Map<String, String> condition) {
        this.condition = condition;
    }

}
